package gdut.jmxywz.action;

import gdut.jmxywz.been.Dissertation;
import gdut.jmxywz.been.DissertationFile;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;



@SuppressWarnings("serial")
public class DissertationFilesAction extends BaseAction {
	
	private String  disId;
	private String  dissertationFileId;
	private String  dissertationFileName;
	private String  dissertationFileContext;
	private String  dissertationFileUrl;
	private List<DissertationFile>  dissertationFile;
	private DissertationFile dissertationFile2;
	

	/***
	 * Delete 删除附件
	 */
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String  Delete() throws Exception{
		
 
        int id = Integer.parseInt(disId);
        int filesId=0;
     
//        boolean b = this.getCommonService().deleteAllFile(id,filesId);
         boolean b = false;
         
         String hql = "from DissertationFile where disId="+id;
         System.out.println("=+++++++ "+dissertationFileId);
         if(!"".equals(dissertationFileId)&&dissertationFileId!=null){
         	filesId = Integer.parseInt(dissertationFileId);
         	
         	hql = hql + " and dissertationFileId = "+filesId;
         }
         String []parem = null;
         
         System.out.println("-------hql------"+hql);
         List i = this.getCommonService().findListByHql(hql, parem);
         
         Iterator iter = i.iterator();
         
         while(iter.hasNext()){
        	 
        	 DissertationFile dissertationfiles = (DissertationFile)iter.next();
        	 
        	 System.out.println("++准备删除文件++");
        	 b=this.getCommonService().deleteObject(dissertationfiles);
        	 
        
        	 System.out.println("-------------删除成功--------------");
        	 
	         String path =ServletActionContext.getRequest().getRealPath("/");
	        
	         System.out.println("----------------"+path);
	         
	         path =path+dissertationfiles.getDissertationFileUrl();
	         
	         System.out.println("++++"+path);
	         
	         boolean b1 = this.deleteFile(path);//输入路径，调用deletefile（）将文件从服务器中删除
	         if(b1){
	        		System.out.println("---从服务器上文件删除成功！");
	        		
	        	}else{
	        		System.out.println("----从服务器上文件删除失败！");
	        		
	        	}   	 
        	 
        	 
         }
         
         
		if(b){
			return "DissertationFilesDelete";
		}
		else{
			return "error";
		}
         
	}
	
	/***
	 * preUpdate 显示将要删除附件的信息
	 */
	
	public  String PreUpdate() throws Exception{
		 
			int  id  = 0 ;
			
			if(!"".equals(dissertationFileId)&&dissertationFileId!=null){
				
				id = Integer.parseInt(dissertationFileId);
			}
			
			dissertationFile2 =(DissertationFile) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.DissertationFile");
			
//			System.out.println("++++++++++++++++++++++++++++++++++");
//			
//			System.out.println("========="+bookmakingFile2.getBookmakingFileName());
//			
//			Bookmaking bookmaking = new Bookmaking();
//			bookmaking =bookmakingFile2.getBookmaking();
//			
//			System.out.println("-------rrr--------"+bookmaking.getBookmakingTitle());
//			
		
		return "DissertationFilesPreUpdate";
	}
	
	
	
	
	
	/***
	 * 
	 * update 修改部件
	 */
	
	
	public String  Update() throws Exception{
		
		int  fileid  = 0 ;
		int  infId = 0; 
		
		DissertationFile dissertationFiles = new  DissertationFile();
		
		if(!"".equals(dissertationFileId)&&dissertationFileId!=null&&!"".equals(disId)&&disId!=null){
			
			fileid = Integer.parseInt(dissertationFileId);
			
			infId = Integer.parseInt(disId);
			
			Dissertation dissertation  = (Dissertation)this.getCommonService().findByIntId(infId, "gdut.jmxywz.been.Dissertation");
			
			dissertationFiles.setDissertationFileId(fileid);
			dissertationFiles.setDissertationFileContext(dissertationFileContext);
			dissertationFiles.setDissertationFileName(dissertationFileName);
			dissertationFiles.setDissertationFileUrl(dissertationFileUrl);
		    
			dissertationFiles.setDissertation(dissertation);
		}
		
	    boolean b= this.getCommonService().updateObject(dissertationFiles);
		
	    if(b){
	    	
	    	System.out.println("附件信息修改成功！");
	    	
	    	return "DissertationFilesUpdate";
	    }else{
	    	
	    	System.out.println("附件信息修改失败！");
	    	
	    	return "error";
	    }
		
	}

//	
//	public  boolean DeleteFiles(String sPath) {   
//		boolean  flag = false;   
//		File   file = new File(sPath);   
//	    // 路径为文件且不为空则进行删除   
//	    if (file.isFile() && file.exists()) {   
//	        file.delete();   
//	        System.out.println("---成功从服务器删除文件---");
//	        flag = true;   
//	    }   
//	    return flag;   
//	}  
//	
	
	
	
	   /**
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
		public  boolean deleteFile(String sPath) {   
			boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}



	public String getDisId() {
		return disId;
	}

	public void setDisId(String disId) {
		this.disId = disId;
	}

	public String getDissertationFileId() {
		return dissertationFileId;
	}

	public void setDissertationFileId(String dissertationFileId) {
		this.dissertationFileId = dissertationFileId;
	}

	public String getDissertationFileName() {
		return dissertationFileName;
	}

	public void setDissertationFileName(String dissertationFileName) {
		this.dissertationFileName = dissertationFileName;
	}

	public String getDissertationFileContext() {
		return dissertationFileContext;
	}

	public void setDissertationFileContext(String dissertationFileContext) {
		this.dissertationFileContext = dissertationFileContext;
	}

	public String getDissertationFileUrl() {
		return dissertationFileUrl;
	}

	public void setDissertationFileUrl(String dissertationFileUrl) {
		this.dissertationFileUrl = dissertationFileUrl;
	}

	public List<DissertationFile> getDissertationFile() {
		return dissertationFile;
	}

	public void setDissertationFile(List<DissertationFile> dissertationFile) {
		this.dissertationFile = dissertationFile;
	}

	public DissertationFile getDissertationFile2() {
		return dissertationFile2;
	}

	public void setDissertationFile2(DissertationFile dissertationFile2) {
		this.dissertationFile2 = dissertationFile2;
	}

		
	
		
		
		
}
